package com.da.web.dorm_room_bed.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.da.common.Result;
import com.da.web.dorm_room.service.DormRoomService;
import com.da.web.dorm_room_bed.entity.DormRoomBed;
import com.da.web.dorm_room_bed.service.DormRoomBedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author da
 * @time 2023/12/10 下午 4:41
 */
@RestController
@RequestMapping("/api/roomBed")
public class DormRoomBedController {
    @Autowired
    private DormRoomBedService dormRoomBedService;
    @Autowired
    private DormRoomService dormRoomService;

    @PutMapping
    public Result edit(@RequestBody DormRoomBed dormRoomBed) {
        //判断编号是否重复
        QueryWrapper<DormRoomBed> query = new QueryWrapper<>();
        query.lambda().eq(DormRoomBed::getBedCode, dormRoomBed.getBedCode());
        DormRoomBed one = dormRoomBedService.getOne(query);
        if (one != null && !Objects.equals(one.getBedId(), dormRoomBed.getBedId())) {
            return Result.error("编号重复");
        }
        boolean b = dormRoomBedService.updateById(dormRoomBed);
        if (b) {
            return Result.success("编辑成功");
        }
        return Result.error("编辑失败");
    }

    @DeleteMapping("/{bedId}")
    public Result delete(@PathVariable("bedId") Long bedId) {
        //判断床位有没有人住,没人住才能删除
        boolean b = dormRoomBedService.removeById(bedId);
        if (b) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

}
